import 'package:bruno/bruno.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:sph_shipper/http/NoLoadingGetApi.dart';

import '../../AppConfig/Config.dart';
import '../../http/api.dart';

class DispatchedDetial extends StatefulWidget {
  const DispatchedDetial({super.key});

  @override
  State<DispatchedDetial> createState() => _DispatchedDetialState();
}

class _DispatchedDetialState extends State<DispatchedDetial> {
  Map dataMap={};
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getDetialAct();
  }
  void getDetialAct(){
    NoLoadinggetApiManager.getData(
      context, // 上下文对象，用于显示提示信息等
      'api/tms/waybill/detail', // 您的API端点
      {}, // 查询参数，如果没有可以传递null
          (responseData)  {
        setState(() {
          dataMap=responseData['data'];
        });
      },
    );
  }
  @override
  Widget build(BuildContext context) {
    final screenSize = MediaQuery.of(context).size;
    return Scaffold(
      backgroundColor: MyColors.backgreyColor,
      appBar: AppBar(
        title: Text("运单详情",style: TextStyle(fontWeight: FontWeight.w300,fontSize: 16.sp,color: Colors.black),),
        centerTitle: true,
        backgroundColor: Color.fromRGBO(251,236,228,1),
        leading: IconButton(icon: Icon(Icons.arrow_back_ios,color: Colors.black,),onPressed: (){Navigator.pop(context);},),
      ),
      body:  Container(
        decoration: BoxDecoration(
          gradient: LinearGradient(
            begin: Alignment.topCenter,
            end: Alignment.bottomCenter,
            colors: [Color.fromRGBO(251,236,228,1),MyColors.backgreyColor, MyColors.backgreyColor],
          ),
        ),
        child: Column(
    children: [
      Expanded(child: Container(
        padding: EdgeInsets.only(left: 10.w,right: 10.w),
        height: screenSize.height-80.h,
        child: ListView(
        children: [
          Text(dataMap['waybill']['status']==1?"未发车":dataMap['waybill']['status']==2?"已发车":dataMap['waybill']['status']==3?"已到达":"已回单",style: TextStyle(color: MyColors.themeColor,fontSize: 28.sp,fontWeight: FontWeight.w600),),
          SizedBox(height: 8.h,),
          Container(
            padding: EdgeInsets.only(left: 10.w,right: 10.w,top: 10.h,bottom: 10.h),
            decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.all(Radius.circular(10))),
            child: Column(
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text("运单号",style: TextStyle(color: Colors.black,fontSize: 14.sp),),
                    Text(dataMap['id'],style: TextStyle(color: Colors.black,fontSize: 14.sp),),
                  ],
                ),
                SizedBox(height: 4.h,),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text("调度员",style: TextStyle(color: Colors.black,fontSize: 14.sp),),
                    Text(dataMap['waybill']['createUser'],style: TextStyle(color: Colors.black,fontSize: 14.sp),),
                  ],
                ),
                SizedBox(height: 4.h,),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text("业务员",style: TextStyle(color: Colors.black,fontSize: 14.sp),),
                    Text(dataMap['waybill']['updateUser'],style: TextStyle(color: Colors.black,fontSize: 14.sp),),
                  ],
                ),
                SizedBox(height: 4.h,),

                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text("运费（元）",style: TextStyle(color: Colors.black,fontSize: 14.sp),),
                    Text(dataMap['waybill']['payableMoney'].toString(),style: TextStyle(color: MyColors.themeColor,fontSize: 14.sp),),
                  ],
                ),
              ],
            ),

          ),
        Container(
          padding: EdgeInsets.only(left: 10.w,right: 10.w,top: 10.h,bottom: 10.h),
          margin: EdgeInsets.only(top: 8.h),
          decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.all(Radius.circular(10))),
          child: Column(
            children: [
              Row(
                children: [
                  Column(
                    children: [
                      Image.asset("lib/Images/OrderList/装货点icon.png",width:20.w ,height:20.h,),
                      Image.asset("lib/Images/OrderList/垂直虚线.png",width:15.w ,height:20.h,),
                      Image.asset("lib/Images/OrderList/卸货点icon.png",width:20.w ,height:20.h,),
                    ],
                  ),
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Container(
                constraints: BoxConstraints(
                  maxWidth:screenSize.width-70.w, // 限制文本最大宽度
                ),
                child: Text(dataMap['waybill']['loadPlaceInfo'][0]['province']+" "+dataMap['waybill']['loadPlaceInfo'][0]['city']+" "+dataMap['waybill']['loadPlaceInfo'][0]['county'],style: TextStyle(color: Colors.black,fontSize: 14.sp,fontWeight: FontWeight.w600,overflow: TextOverflow.ellipsis),),
              ),
              Container(
                constraints: BoxConstraints(
                  maxWidth:screenSize.width-70.w, // 限制文本最大宽度
                ),
                child: Text(dataMap['waybill']['loadPlaceInfo'][0]['address'],style: TextStyle(color: MyColors.textColor,fontSize: 14.sp,overflow: TextOverflow.ellipsis),),
              ),

              SizedBox(height: 4.h,),
              Container(
                constraints: BoxConstraints(
                  maxWidth:screenSize.width-70.w, // 限制文本最大宽度
                ),
                child: Text(dataMap['waybill']['unloadPlaceInfo'][0]['province']+" "+dataMap['waybill']['unloadPlaceInfo'][0]['city']+" "+dataMap['waybill']['unloadPlaceInfo'][0]['county'],style: TextStyle(color: Colors.black,fontSize: 14.sp,fontWeight: FontWeight.w600,overflow: TextOverflow.ellipsis),),
              ),
              Container(
                constraints: BoxConstraints(
                  maxWidth:screenSize.width-70.w, // 限制文本最大宽度
                ),
                child: Text(dataMap['waybill']['unloadPlaceInfo'][0]['address'],style: TextStyle(color: MyColors.textColor,fontSize: 14.sp,overflow: TextOverflow.ellipsis),),
              ),

            ],
          ),

              ],
          ),




                ],
              )

          ),
        Container(
          padding: EdgeInsets.only(left: 10.w,right: 10.w,top: 10.h,bottom: 10.h),
          margin: EdgeInsets.only(top: 8.h),
          decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.all(Radius.circular(10))),
          child: Column(
            children: [
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text("司机信息",style: TextStyle(color: Colors.black,fontSize: 14.sp),),
                  Text(dataMap['waybill']['driverName']+"丨"+dataMap['waybill']['driverPhone'],style: TextStyle(color: Colors.black,fontSize: 14.sp),),
                ],
              ),
              SizedBox(height: 4.h,),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text("车辆信息",style: TextStyle(color: Colors.black,fontSize: 14.sp),),
                  Text(dataMap['waybill']['vehicleNum'],style: TextStyle(color: Colors.black,fontSize: 14.sp),),
                ],
              ),
              SizedBox(height: 4.h,),
              Row(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text("备注",style: TextStyle(color: Colors.black,fontSize: 14.sp),),
                  Container(
                    constraints: BoxConstraints(
                      maxWidth:screenSize.width-170.w, // 限制文本最大宽度
                    ),
                    child: Text(dataMap['waybill']['remark'],style: TextStyle(color:Colors.black,fontSize: 14.sp),),
                  ),
                ],
              ),

            ],
          ),
        ),
        Container(
          padding: EdgeInsets.only(left: 10.w,right: 10.w,top: 10.h,bottom: 10.h),
          margin: EdgeInsets.only(top: 8.h),
          decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.all(Radius.circular(10))),
          child: Column(
            children: [
              // Row(
              //   mainAxisAlignment: MainAxisAlignment.spaceBetween,
              //   children: [
              //     Text("客户名称",style: TextStyle(color: Colors.black,fontSize: 14.sp),),
              //     Container(
              //       constraints: BoxConstraints(
              //         maxWidth:screenSize.width-170.w, // 限制文本最大宽度
              //       ),
              //       child: Text("万华",style: TextStyle(color: Colors.black,fontSize: 14.sp),),
              //     ),
              //   ],
              // ),
              // SizedBox(height: 4.h,),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text("订单号",style: TextStyle(color: Colors.black,fontSize: 14.sp),),
                  Container(
                    constraints: BoxConstraints(
                      maxWidth:screenSize.width-170.w, // 限制文本最大宽度
                    ),
                    child: Text(dataMap['orderIdList'],style: TextStyle(color: Colors.black,fontSize: 14.sp),),
                  ),
                ],
              ),
              // SizedBox(height: 4.h,),
              // Row(
              //   mainAxisAlignment: MainAxisAlignment.spaceBetween,
              //   children: [
              //     Text("提货单号",style: TextStyle(color: Colors.black,fontSize: 14.sp),),
              //     Container(
              //       constraints: BoxConstraints(
              //         maxWidth:screenSize.width-170.w, // 限制文本最大宽度
              //       ),
              //       child: Text("123231123123",style: TextStyle(color: Colors.black,fontSize: 14.sp),),
              //     ),
              //   ],
              // ),
              SizedBox(height: 4.h,),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text("订单时间",style: TextStyle(color: Colors.black,fontSize: 14.sp),),
                  Container(
                    constraints: BoxConstraints(
                      maxWidth:screenSize.width-170.w, // 限制文本最大宽度
                    ),
                    child: Text(dataMap['waybill']['createTime'],style: TextStyle(color: Colors.black,fontSize: 14.sp),),
                  ),
                ],
              ),
              SizedBox(height: 4.h,),
            ],
          ),
        ),
          ],
          ),
          ),
          ),
      Container(
        height: 80.h,
        color: Colors.white,
        width: screenSize.width,
        padding: EdgeInsets.only(bottom: 35.h),
        child:   Row(
          mainAxisAlignment: MainAxisAlignment.end,
          children: [
            if(dataMap['waybill']['status']==1)
            GestureDetector(
              child:   BrnTagCustom.buildBorderTag(
                tagText: '发运',
                textColor:  MyColors.themeColor,
                borderColor: MyColors.themeColor,
                borderWidth: 1,
                fontSize: 14.sp,
                tagBorderRadius:BorderRadius.all(Radius.circular(30)),
                textPadding: EdgeInsets.only(left: 15.w,right: 15.w,top: 4.h,bottom: 4.h),
              ),
              onTap: (){
                ApiManager.postData(
                  context,
                  'api/user/waybill/status',
                  {
                    "waybillIdList":dataMap['waybill']['id'],
                    "status":"STARTED",
                  },
                      (responseData) {
                    BrnToast.show("操作成功", context);
                    getDetialAct();
                  },
                );
              },
            ),
            if(dataMap['waybill']['status']==2)
            SizedBox(width: 10.w,),
            if(dataMap['waybill']['status']==2)
            GestureDetector(
              child:   BrnTagCustom.buildBorderTag(
                tagText: '到达',
                textColor:  MyColors.themeColor,
                borderColor: MyColors.themeColor,
                borderWidth: 1,
                fontSize: 14.sp,
                tagBorderRadius:BorderRadius.all(Radius.circular(30)),
                textPadding: EdgeInsets.only(left: 15.w,right: 15.w,top: 4.h,bottom: 4.h),
              ),
              onTap: (){
                ApiManager.postData(
                  context,
                  'api/user/waybill/status',
                  {
                    "waybillIdList":dataMap['waybill']['id'],
                    "status":"ENDED",
                  },
                      (responseData) {
                    BrnToast.show("操作成功", context);
                    getDetialAct();
                  },
                );
              },
            ),
            if(dataMap['waybill']['status']==3)
            SizedBox(width: 10.w,),
            if(dataMap['waybill']['status']==3)
            GestureDetector(
              child:   BrnTagCustom.buildBorderTag(
                tagText: '回单',
                textColor:  MyColors.themeColor,
                borderColor: MyColors.themeColor,
                borderWidth: 1,
                fontSize: 14.sp,
                tagBorderRadius:BorderRadius.all(Radius.circular(30)),
                textPadding: EdgeInsets.only(left: 15.w,right: 15.w,top: 4.h,bottom: 4.h),
              ),
              onTap: (){
                ApiManager.postData(
                  context,
                  'api/user/waybill/status',
                  {
                    "waybillIdList":dataMap['waybill']['id'],
                    "status":"RETURNED",
                  },
                      (responseData) {
                    BrnToast.show("操作成功", context);
                    getDetialAct();
                  },
                );
              },
            ),
            SizedBox(width: 10.w,)
          ],
        ),
      )
          ],
    ),
      ),
    );
  }
}
